package com.gwnet.shop.service.impl;

import java.util.List;

import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.mapper.SysTenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gwnet.shop.mapper.UserCollectTenantMapper;
import com.gwnet.shop.domain.UserCollectTenant;
import com.gwnet.shop.service.IUserCollectTenantService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户收藏店铺Service业务层处理
 * 
 * @author 张万鹏
 * @date 2024-08-01
 */
@Service
public class UserCollectTenantServiceImpl implements IUserCollectTenantService 
{
    @Resource
    private UserCollectTenantMapper userCollectTenantMapper;

    @Resource
    private SysTenantMapper tenantMapper;

    /**
     * 查询用户收藏店铺
     * @param userCollectTenant 用户收藏店铺主键
     * @return 用户收藏店铺
     */
    @Override
    public UserCollectTenant selectUserCollectTenantByUserIdAndTenantId(UserCollectTenant userCollectTenant) {
        return userCollectTenantMapper.selectUserCollectTenantByUserIdAndTenantId(userCollectTenant);
    }

    /**
     * 查询用户收藏店铺列表
     * 
     * @param userCollectTenant 用户收藏店铺
     * @return 用户收藏店铺
     */
    @Override
    public List<UserCollectTenant> selectUserCollectTenantList(UserCollectTenant userCollectTenant)
    {
        return userCollectTenantMapper.selectUserCollectTenantList(userCollectTenant);
    }

    /**
     * 新增用户收藏店铺
     * 
     * @param userCollectTenant 用户收藏店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUserCollectTenant(UserCollectTenant userCollectTenant)
    {
        try{
            userCollectTenant.setCreateTime(DateUtils.getNowDate());
            int rows = userCollectTenantMapper.insertUserCollectTenant(userCollectTenant);
            tenantMapper.changeCollectNumPlusById(userCollectTenant.getTenantId());
            return rows;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("您已收藏过此店铺!");
        }
    }

    /**
     * 删除用户收藏店铺
     *
     * @param userCollectTenant 用户收藏店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserCollectTenantByUserIdAndTenantId(UserCollectTenant userCollectTenant) {
        tenantMapper.changeCollectNumMinusById(userCollectTenant.getTenantId());
        return userCollectTenantMapper.deleteUserCollectTenantByUserIdAndTenantId(userCollectTenant);
    }


}
